/**
 * 
 */
package de.oszimtcc.timetableadapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import de.oszimtcc.timetable.R;
import de.oszimtcc.timetable.TimetableApplication;
import de.oszimtcc.timetablecore.Block;
import de.oszimtcc.timetablecore.Block.BlockType;
import de.oszimtcc.timetablecore.Day;
import de.oszimtcc.timetablelistener.HideAfterEndAnimationListener;


/**
 * @author Kooki
 *
 */
public class DayAdapter extends ArrayAdapter<Day>
{
	private Context mContext;
	private List<Block> dayList = new ArrayList<Block>();
	private int dayIndex;
	
	public DayAdapter(Context pContext, int pActualShownDay, List<Block> pDayList) 
	{
		super(pContext, 0);
		dayIndex = pActualShownDay;
		mContext = pContext;
		this.dayList = pDayList;
	}
	
	@Override
	public int getCount() 
	{
		return (dayList != null ? dayList.size() : 0);
	}

	public Block getBlock(int pPosition) 
	{
		if(dayList == null || dayList.isEmpty() || pPosition > dayList.size() - 1) 
		{
			return null;
		}
		
		return dayList.get(pPosition);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		View v = new View(mContext);
		if (convertView == null) 
		{
			LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.block, null);
            
		} 
		else 
		{
			v = convertView;
		}
		
		Block block = dayList.get(position);
		v.setBackgroundResource(block.GetBlockType() == BlockType.Standard ? R.drawable.normal : block.GetBlockType() == BlockType.Changed ? R.drawable.change : R.drawable.cancel);
		
		TextView blockNumber = (TextView) v.findViewById(R.id.blockNumber);
		TextView blockStart = (TextView) v.findViewById(R.id.blockStart);
		TextView blockEnd = (TextView) v.findViewById(R.id.blockEnd);
		TextView blockSubject = (TextView) v.findViewById(R.id.blockSubject);
		TextView blockTeacher = (TextView) v.findViewById(R.id.blockTeacher);
		TextView blockRoom = (TextView) v.findViewById(R.id.blockRoom);
		ImageView newChangePicture = (ImageView) v.findViewById(R.id.blockHasNewChangePicture);
		
		if(block.GetHasNewChanges())
			newChangePicture.setVisibility(View.VISIBLE);
		else
			newChangePicture.setVisibility(View.INVISIBLE);
		if(block.GetBlockType() == BlockType.Cancellation)
		{
			blockNumber.setText(""+block.GetBlockNumber());
			blockStart.setText(block.GetStartTime());
			blockEnd.setText(block.GetEndTime());
			blockSubject.setText("");
			blockTeacher.setText("");
			blockRoom.setText("Ausfall !!");
			
			setTextColorRecursive(v);
			blockNumber.setTextColor(Color.BLACK);
		}
		else if(block.GetBlockType() == BlockType.Standard || block.GetBlockType() == BlockType.Changed)
		{
			blockNumber.setText(""+block.GetBlockNumber());
			blockStart.setText(block.GetStartTime());
			blockEnd.setText(block.GetEndTime());
			blockSubject.setText(block.GetSubject());
			blockTeacher.setText(block.GetTeacher());
			blockRoom.setText(block.GetRoom());
			
			setTextColorRecursive(v);
			blockNumber.setTextColor(Color.BLACK);
		}
		else if(block.GetBlockType() == BlockType.Free)
		{	
			v.setBackgroundColor(Color.TRANSPARENT);
			//((ViewGroup)v).setVisibility(View.INVISIBLE);
			ClearBlockViewsRecursive(v);
		}

		v.setTag(block);
		return v;
	}
	
	public ImageView getBlockChangePicture(View pConverted)
	{
		View v = new View(mContext);
		v = pConverted; 
		return (ImageView) v.findViewById(R.id.blockHasNewChangePicture);
	}

	protected void setTextColorRecursive(View v) 
	{
		if(v instanceof ViewGroup) 
		{
			for(int i=0; i<((ViewGroup) v).getChildCount(); i++) 
			{
				View iv = ((ViewGroup) v).getChildAt(i);
				if(iv instanceof TextView) 
				{
					TextView text = (TextView)iv;
					if(!text.getText().toString().contains("_"))
					{
						text.setTextColor(Color.BLACK);
					}
					if(text.getText().toString().contains("c_"))
					{
						text.setTextColor(Color.RED);
						text.setText(text.getText().toString().replace("c_", ""));
					}
					else if(text.getText().toString().contains("n_"))
					{
						text.setTextColor(Color.BLACK);
						text.setText(text.getText().toString().replace("n_", ""));
					}
				} 
				else if(iv instanceof ViewGroup) 
				{
					setTextColorRecursive(iv);
				}  
			}
		}
	}
	
	protected void ClearBlockViewsRecursive(View v) 
	{
		if(v instanceof ViewGroup) 
		{
			for(int i=0; i<((ViewGroup) v).getChildCount(); i++) 
			{
				View iv = ((ViewGroup) v).getChildAt(i);
				if(iv instanceof TextView) 
				{
					TextView text = (TextView)iv;
					text.setText("");
				}
				if(iv instanceof ImageView) 
				{
					ImageView image = (ImageView)iv;
					image.setVisibility(View.INVISIBLE);
				}
				else if(iv instanceof ViewGroup) 
				{
					ClearBlockViewsRecursive(iv);
				}  
			}
		}
	}
	
	public List<Block> getData() 
	{
		return dayList;
	}

	public void setData(List<Block> pDayList) 
	{
		this.dayList = pDayList;
	}
}
